<?
require_once("../config/dbconn.php");require_once("../config/powercls.php");require_once("../config/pagecls.php");

if ($action=='cancel'){
	$sql="select * from {$db_prefix}gp_market where id='$id'";
	$rs=$db->get_one($sql);
	if ($rs['state']!=0){
		echo "<script>alert('交易状态错误');history.back();</script>";exit();
	}
	$sqlgx="update {$db_prefix}gp_market set state=2 where id='$id'";
	$db->query($sqlgx);
	//买入报单币
	if($rs['type']==1){
		$sygpprice=$rs['num']*$rs['price'];
		//退还会员的电子股金额
		$sqlq="update {$db_prefix}users set gpprice=gpprice+'$sygpprice' where username='".$rs['username']."'";
		$db->query($sqlq);
		$e_userid=$_SESSION["sys_userid"];$e_price=$sygpprice;$e_type=3;$e_ptype=4;$e_addtime=$curtime;$e_memo="取消出售报单币";
		hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
	}
	//售出报单币
	if($rs['type']==2){
		$sygpnum=$rs['num'];
		//退还会员的原始股量
		$sqlq="update {$db_prefix}users set gpnum=gpnum+'$sygpnum' where username='".$rs['username']."'";
		$db->query($sqlq);
	}
	echo "<script>alert('交易已取消');location.href='gupiao_clear.php?pageno={$pageno}';</script>";exit();
}

if($action=='clearall'){
	//清理交易盘
	$sql="select * from {$db_prefix}gp_market where num>0";
	$result=$db->query($sql);
	while($rs=$db->fetch_array($result)){
		if ($rs['state']!=0){
			continue;
			/*echo "<script>alert('交易状态错误');history.back();</script>";exit();*/
		}
		$sqlgx="update {$db_prefix}gp_market set state=2 where id='".$rs['id']."'";
		$db->query($sqlgx);
		//买入报单币
		if($rs['type']==1){
			$sygpprice=$rs['num']*$rs['price'];
			//退还会员的电子股金额
			$sqlq="update {$db_prefix}users set gpprice=gpprice+'$sygpprice' where username='".$rs['username']."'";
			$db->query($sqlq);
			$e_userid=$_SESSION["sys_userid"];$e_price=$sygpprice;$e_type=3;$e_ptype=4;$e_addtime=$curtime;$e_memo="取消出售报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		//售出报单币
		if($rs['type']==2){
			$sygpnum=$rs['num'];
			//退还会员的原始股量
			$sqlq="update {$db_prefix}users set gpnum=gpnum+'$sygpnum' where username='".$rs['username']."'";
			$db->query($sqlq);
		}
	}
	echo "<script>alert('交易已取消');location.href='gupiao_clear.php?pageno={$pageno}';</script>";exit();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	
}
td{
font-size:12px;
}
-->
</style>
<script language="javascript" type="text/javascript" src="../calendar/WdatePicker.js"></script>
<link href="images/skin.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
-->
</style>
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" height="29" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td width="935" height="29" valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt">报单币交易盘</div></td>
      </tr>
    </table></td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td height="71" valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="13" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
                <tr>
                  <td align="center" class="left_bt2"><form id="form1" name="form1" method="post" action="?act=query">
                    会员编号：
                        <input name="username" type="text" id="username" size="10" />
                        交易方式:
                        <label>
      <select name="type">
        <option value="">请选择</option>
        <option value="1">买入</option>
        <option value="2">售出</option>
      </select>
      </label>
                    价格：
  <label>
  <input name="price" type="text" id="price" size="5" />
  </label><br>
                    交易状态:
  <label>
  <select name="state" id="state">
    <option value="">所有状态</option>
    <?
		foreach($gupiaostateary as $k1=>$v1){
			echo "<option value='{$k1}'>{$v1}</option>";
		}
		?>
  </select>
    
  </label>
  <label> 开始
  <input name="time1" type="text" id="time1" size="8" onClick="WdatePicker()" />
    到
  <input name="time2" type="text" id="time2" size="8" onClick="WdatePicker()" />
  <input type="submit" name="Submit" value="查询" />
  </label>
                                    </form></td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td height="30"><table width="100%" height="60" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td height="30" align="center" bgcolor="#f2f2f2" ><span class="STYLE1">交易编号</span></td>
                  <td align="center" bgcolor="#f2f2f2" >会员编号</td>
                  <td align="center" bgcolor="#f2f2f2" >交易方式</td>
                  <td align="center" bgcolor="#f2f2f2" >交易量</td>
                  <td align="center" bgcolor="#f2f2f2" >成交量</td>
                  <td align="center" bgcolor="#f2f2f2" >交易价格</td>
                  <td align="center" bgcolor="#f2f2f2" >开盘价格</td>
                  <td align="center" bgcolor="#f2f2f2" >交易日期</td>
                  <td align="center" bgcolor="#f2f2f2" >成交日期</td>
                  <td align="center" bgcolor="#f2f2f2" ><span class="STYLE1">交易状态</span></td>
                  <td align="center" bgcolor="#f2f2f2" >操作</td>
                  </tr>
				  <?
  $query='';
  if ($act=='query'){
  	if (trim($username)!='') $query.=" and username='".trim($username)."'";
  	if ($type) $query.=" and type='$type'";
						if (trim($price)!='') $query.=" and price='$price'";
						if ($state) $query.=" and state='$state'";
						if(trim($time1)!='') $query.=" and datediff('".trim($time1)."',from_unixtime(addtime))>=0";
						if(trim($time2)!='') $query.=" and datediff(from_unixtime(jztime),'".trim($time2)."')>=0";
						if ($guoqi){
							if ($guoqi==1){
								$query.=" and datediff(from_unixtime('".time()."'),from_unixtime(jztime))>0";
							}
						}
  }
  
  $sql="select count(id) as c  from {$db_prefix}gp_market where num>0";
  if ($query!='') $sql.=$query;
  $rs=$db->get_one($sql);
  $page=new pagecls(20,intval($rs['c']));
  $sql="select * from {$db_prefix}gp_market where num>0";
  if ($query!='') $sql.=$query;
  $sql.=" order by id desc limit {$page->pastnum},{$page->pagesize}";
  $result=$db->query($sql);
  while($rs=$db->fetch_array($result)){
  ?>
                <tr>
                  <td height="30" align="center"><? echo $rs["id"]?></td>
                  <td align="center"><? echo $rs["username"]?></td>
                  <td align="center"><span class="STYLE1">
                    <? if ($rs["type"]==1) echo "买入";else echo "售出";?>
                  </span></td>
                  <td align="center"><span class="STYLE1"><? echo $rs["num1"]?></span></td>
                  <td align="center"><span class="STYLE1"><? echo $rs["num1"]-$rs["num"]?></span></td>
                  <td align="center"><span class="STYLE1"><? echo $rs["price"]?></span></td>
                  <td align="center"><span class="STYLE1"><?
				  //获取哪一天的开盘价格
				  $sqlkp="select * from {$db_prefix}gupiaokp where kpdate='".date("Y-m-d",$rs['addtime'])."'";
				  $rskp=$db->get_one($sqlkp);
				  echo $rskp['kpprice'];
				  ?></span></td>
                  <td align="center"><span class="STYLE1"><? echo date("Y-m-d",$rs['addtime'])?></span></td>
                  <td align="center"><span class="STYLE1">
                    <? if ($rs['cjtime']>0) echo date("Y-m-d",$rs["cjtime"])?>
                  </span></td>
                  <td align="center"><?=$gupiaostateary[$rs['state']]?></td>
                  <td align="center">
				  <?
				  if($rs['state']==0){
				  ?>
				  <a href="?action=cancel&id=<?=$rs['id']?>&pageno=<?=$pageno?>" onClick="return confirm('确定要取消吗？')">取消</a>
				  <?
				  }
				  ?>
				  </td>
                  </tr>
				   <?
  }
  $db->free_result($result);
  ?>
              </table></td>
            </tr>
            
            <tr>
              <td height="30">&nbsp;</td>
            </tr>
            <tr>
              <td height="30" align="right">每页<?=$page->pagesize?>条 第<?=$page->pageno?>/<?=$page->pagenum?>页 共<?=$page->recnum?>条 <a href='?<?=$page->url?>1'><img src="image1/first.gif" border="0"></a>   <a href='?<?=$page->url.($page->pageno-1)?>'><img src="image1/back.gif" border="0"></a>  <a href='?<?=$page->url.($page->pageno+1)?>'><img src="image1/next.gif" border="0"></a>   <a href='?<?=$page->url.$page->pagenum?>'><img src="image1/last.gif" border="0"></a></td>
              </tr>
            <tr>
              <td height="30"><a href="?action=clearall" onClick="return confirm('确定要清理交易盘吗？')">清理交易盘</a></td>
            </tr>
          </table></td>
      </tr>
    </table></td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
      <td height="17" valign="top" background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17" /></td>
    <td background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>

</body>
</html>
